<template>
  <el-dialog :title="starTitle" :visible.sync="starOpen" width="80%" append-to-body @close="closeDialog">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
             class="main-search">
      <el-form-item label="客户姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入客户姓名" clearable @keyup.enter.native="handleQuery"
                  style="width: 160px"/>
      </el-form-item>
      <el-form-item label="部门" prop="childrenDept" v-if="currentType == 'teamCustomer'">
        <treeselect v-model="queryParams.childrenDept" style="" :options="deptOptions" :show-count="true"
                    placeholder="请选择归属部门"
                    @keyup.enter.native="handleQuery"
                    :normalizer="normalizer"
                    @input="deptChange(queryParams.childrenDept, 'childrenDept')"/>
      </el-form-item>
      <el-form-item label="顾问" prop="busUserId" v-if="currentType == 'teamCustomer'">
        <el-select v-model="queryParams.busUserId" placeholder="不限" clearable style="width: 160px" filterable>
          <el-option
              v-for="dict in userOptions"
              :key="dict.userId"
              :label="dict.nickName + '（' + dict.dept.deptName + '）'"
              :value="dict.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户电话" prop="tel">
        <el-input v-model="queryParams.tel" placeholder="请输入客户电话" clearable @keyup.enter.native="handleQuery"
                  style="width: 160px"/>
      </el-form-item>
      <el-form-item label="客户状态" prop="customerStatus">
        <el-select v-model="queryParams.customerStatus" placeholder="请选择客户状态" clearable style="width: 160px">
          <el-option v-for="dict in dict.type.customer_status" :key="dict.value"
                     :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="公司状态" prop="companyStatus">
        <el-select v-model="queryParams.companyStatus" placeholder="请选择公司状态" clearable style="width: 160px">
          <el-option v-for="dict in dict.type.company_status" :key="dict.value"
                     :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="星级" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择星级" clearable style="width: 160px">
          <el-option v-for="dict in dict.type.level_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="贷款类型" prop="loanType">
        <el-select v-model="queryParams.loanType" placeholder="请选择贷款类型" clearable style="width: 160px">
          <el-option v-for="dict in dict.type.loan_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="未更新" prop="notUpdateDay">
        <el-input v-model="queryParams.notUpdateDay" type="number" placeholder="未更新超过天数" clearable
                  @keyup.enter.native="handleQuery"
                  style="width: 160px"/>
      </el-form-item>
      <el-form-item label="时间" prop="timeInfo">
        <el-select v-model="queryParams.timeType" placeholder="不限" clearable style="width: 80px">
          <el-option
              v-for="dict in dict.type.time_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
        <el-select v-model="queryParams.applyTimeScope" placeholder="不限" clearable style="width: 80px"
                   @change="applyTimeScopeChange">
          <el-option
              v-for="dict in dict.type.apply_time_scope"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime" v-if="queryParams.applyTimeScope == 99">
        <el-date-picker
            v-model="queryParams.startTime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            :clearable="false"
            :editable="false"
            placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime" v-if="queryParams.applyTimeScope == 99">
        <el-date-picker
            v-model="queryParams.endTime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            :clearable="false"
            :editable="false"
            placeholder="请选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="重要客户" prop="vip">
        <el-select v-model="queryParams.vip" placeholder="-不限-" clearable style="width: 160px">
          <el-option
              v-for="dict in dict.type.is_vip"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="贷款额度" prop="applyMoney">
        <el-select v-model="queryParams.applyMoney" placeholder="-不限-" clearable style="width: 160px">
          <el-option
              v-for="dict in dict.type.quota_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="bz">
        <el-input v-model="queryParams.bz" placeholder="-不限-" clearable @keyup.enter.native="handleQuery"
                  style="width: 160px"/>
      </el-form-item>
      <el-form-item label="房产" prop="house">
        <el-select v-model="queryParams.house" placeholder="-不限-" clearable>
          <el-option
              v-for="dict in dict.type.house_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <!--        <el-select v-model="queryParams.source" placeholder="-不限-" clearable style="width: 160px">-->
        <!--          <el-option-->
        <!--            v-for="dict in dict.type.source_type"-->
        <!--            :key="dict.value"-->
        <!--            :label="dict.label"-->
        <!--            :value="dict.value"-->
        <!--          />-->
        <!--        </el-select>-->
        <el-input v-model="queryParams.source" placeholder="-不限-" clearable @keyup.enter.native="handleQuery"
                  style="width: 160px"/>
      </el-form-item>
      <el-form-item label="公积金" prop="fund">
        <el-select v-model="queryParams.fund" placeholder="-不限-" clearable style="width: 160px">
          <el-option
              v-for="dict in dict.type.fund_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="再审件" prop="revieweFlag">
        <el-select v-model="queryParams.revieweFlag" placeholder="-不限-" clearable style="width: 160px">
          <el-option
              v-for="dict in dict.type.is_vip"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="批次" prop="batchLogo" v-if="currentType == 'teamCustomer'">
        <el-input v-model="queryParams.batchLogo" placeholder="-不限-" clearable @keyup.enter.native="handleQuery"
                  style="width: 160px"/>
      </el-form-item>
      <el-form-item label="渠道API" prop="channelId">
        <el-select v-model="queryParams.channelId" placeholder="-不限-" clearable style="width: 160px">
          <el-option
              v-for="dict in this.channelDataList"
              :key="dict.id + ''"
              :label="dict.channelName"
              :value="dict.id + ''"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            size="mini"
            @click="handlePull"
            v-hasPermi="['customer:myCustomer:customerPull']"
            v-if="currentType == 'myCustomer'"
        >查看客户池
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            size="mini"
            @click="customerReturn('1')"
            v-if="currentType == 'myCustomer'"
            v-hasPermi="['customer:myCustomer:customerReturn']"
        >批量退回
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            size="mini"
            @click="customerReturn('1')"
            v-if="currentType == 'claim'"
            v-hasPermi="['customer:repeat:customerReturn']"
        >批量退回
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            size="mini"
            @click="handlePLRL"
            v-if="false && currentType == 'claim'"
            v-hasPermi="['customer:repeat:rl']"
        >批量认领
        </el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            size="mini"
            @click="bathHandleTransfer"
            v-if="currentType == 'teamCustomer'"
            v-hasPermi="['customer:teamCustomer:transfer']"
        >批量转移
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"
                     @queryTable="getList(this.currentType, this.starType)"></right-toolbar>
    </el-row>
    <!--    <el-card class="box-card">-->
    <!--      <div class="text item">-->
    <!--        客户提醒：你有待跟进客户条,-->
    <!--        超过1天3星待签约 {{ this.star.three }} 条,-->
    <!--        超过3天2,4,5,6星待签约 {{ this.star.other }} 条-->
    <!--        超过7天1星待签约 {{ this.star.one }} 条,-->
    <!--      </div>-->
    <!--    </el-card>-->

    <!--       -->
    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange"
              @cell-click="cellClick" :height="550"
              @sort-change="sortChange"
              :default-sort="{prop: 'lastRemarkTime', order: 'descending'}">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id" v-if="false"/>
      <el-table-column label="姓名" align="center" prop="name" width="100px" fixed="left"/>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sex_type" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="customerStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_status" :value="scope.row.customerStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="公司状态" align="center" prop="companyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.company_status" :value="scope.row.companyStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="星级" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.level_type" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age"/>
      <el-table-column label="备注" align="center" prop="bz" width="400px">
        <template slot-scope="scope">
          <div class="messageCard" v-html="scope.row.bz">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="lastRemarkTime" width="100"
                       sortable="custom"
                       :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastRemarkTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="顾问" align="center" prop="busName"/>
      <el-table-column label="部门" align="center" prop="deptName"/>
      <el-table-column label="房" align="center" prop="house">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_type" :value="scope.row.house"/>
        </template>
      </el-table-column>
      <el-table-column label="车" align="center" prop="car">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.car_type" :value="scope.row.car"/>
        </template>
      </el-table-column>
      <el-table-column label="保单" align="center" prop="warranty">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.warranty_type" :value="scope.row.warranty"/>
        </template>
      </el-table-column>
      <el-table-column label="社保" align="center" prop="insurance">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.insurance_type" :value="scope.row.insurance"/>
        </template>
      </el-table-column>
      <el-table-column label="公积金" align="center" prop="fund">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fund_type" :value="scope.row.fund"/>
        </template>
      </el-table-column>
      <el-table-column label="申请额度" align="center" prop="applyMoney"/>
      <el-table-column label="进系统时间" align="center" prop="realityApplyTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.realityApplyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="贷款类型" align="center" prop="loanType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.loan_type" :value="scope.row.loanType"/>
        </template>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="source">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.source_type" :value="scope.row.source"/>
        </template>
      </el-table-column>
      <el-table-column label="批次" align="center" prop="batchLogo" v-if="currentType == 'teamCustomer'">
      </el-table-column>
      <!--      <el-table-column label="城市" align="center" prop="regionName"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180px">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click.stop="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['customer:myCustomer:update']"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleUpdate(scope.row)"
              v-hasPermi="['customer:myCustomer:update']"
              v-if="currentType == 'myCustomer'"
          >修改
          </el-button>

          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleUpdate(scope.row)"
              v-hasPermi="['team:customer:update']"
              v-if="currentType == 'teamCustomer'"
          >修改
          </el-button>

          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleUpdate(scope.row)"
              v-hasPermi="['customer:repeat:update']"
              v-if="currentType == 'claim'"
          >修改
          </el-button>

          <!-- 我的客户退回          -->
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleReturn(scope.row)"
              v-if="currentType == 'claim'"
              v-hasPermi="['customer:repeat:customerReturn']"
          >退回
          </el-button>

          <!-- 再分配客户退回          -->
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleReturn(scope.row)"
              v-if="currentType == 'myCustomer'"
              v-hasPermi="['customer:myCustomer:customerReturn']"
          >退回
          </el-button>

          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleTransfer(scope.row)"
              v-if="currentType == 'teamCustomer'"
              v-hasPermi="['customer:teamCustomer:transfer']"
          >转移
          </el-button>

          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleRL(scope.row)"
              v-if="false && scope.row.companyStatus == 3 && currentType == 'claim'"
              v-hasPermi="['customer:repeat:rl']"
          >认领
          </el-button>

          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click.stop="handleRecordBZ(scope.row)"
              v-if="currentType == 'myCustomer'"
              v-hasPermi="['customer:myCustomer:record']"
          >记录
          </el-button>

          <el-button
              size="mini"
              type="text"
              @click.stop="upDoorInfo(scope.row)"
              v-if="currentType == 'myCustomer'"
              v-hasPermi="['customer:myCustomer:up']"
          >上门
          </el-button>
          <el-button
              size="mini"
              type="text"
              @click.stop="inviteInfo(scope.row)"
              v-if="currentType == 'myCustomer'"
              v-hasPermi="['customer:myCustomer:yy']"
          >邀约
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            @click.stop="handleMx(scope.row)"-->
          <!--          >明细-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList(currentType, starType)"
    />

    <!-- 记录每次的流程并更新时间信息 -->
    <el-dialog :title="title" :visible.sync="bzOpen" width="1000px" append-to-body>
      <el-form ref="bzForm" :model="bzForm" :rules="bzRules" label-width="80px">
        <el-form-item label="记录" prop="bz">
          <el-input v-model="bzForm.bz" placeholder="请填写记录信息"/>
        </el-form-item>
        <el-form-item label="沟通记录" prop="remark">
          <div class="messageCard" v-html="bzForm.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitRecord">确 定</el-button>
        <el-button @click="bzCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 记录每次的流程更新时间信息 -->
    <el-dialog :title="title" :visible.sync="mxOpen" width="1000px" append-to-body>
      <el-form ref="mxForm" :model="mxForm" label-width="80px">
        <el-form-item label="客户姓名" prop="name">
          <el-input v-model="mxForm.name" placeholder="请输入姓名" disabled/>
        </el-form-item>
        <el-form-item label="客户电话" prop="tel">
          <el-input v-model="mxForm.tel" placeholder="请输入客户电话" disabled/>
        </el-form-item>
        <el-form-item label="分配记录" prop="lsfpRecord">
          <div class="messageCard" v-html="mxForm.lsfpRecord" disabled/>
        </el-form-item>
        <el-form-item label="沟通记录" prop="lsbzRecord">
          <div class="messageCard" v-html="mxForm.lsbzRecord" disabled/>
        </el-form-item>
      </el-form>
      <el-button size="mini" plain type="primary" @click="handlePrevious">上一篇</el-button>
      <el-button size="mini" plain type="primary" @click="handleNext">下一篇</el-button>
    </el-dialog>

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body @close="ecCloseDialog">
      <el-header style="text-align: center; font-size: 12px;height: 40px;">
        <el-button size="mini" plain type="primary" @click="handlePrevious">上一篇</el-button>
        <el-button size="mini" plain type="primary" @click="handleNext">下一篇</el-button>
        <!-- :loading="buttonLoading"        -->
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">关闭</el-button>
        <el-button size="mini" plain type="primary" @click="customerReturn('3')"
                   v-if="currentType == 'myCustomer'"
                   v-hasPermi="['customer:myCustomer:customerReturn']">退 回
        </el-button>
        <el-button size="mini" plain type="primary" @click="customerReturn('3')"
                   v-if="currentType == 'claim'"
                   v-hasPermi="['customer:repeat:customerReturn']">退 回

        </el-button>
        <el-button size="mini" plain type="primary" @click="tanHandleRl()"
                   v-if="currentType == 'claim'"
                   v-hasPermi="['customer:repeat:rl']">认 领
        </el-button>

        <!--        <el-button size="mini" plain type="primary" @click="customerReturn('3')">退 回</el-button>-->
        <!--        <el-button size="mini" plain type="primary" @click="customerReturn('3')">退 回</el-button>-->
      </el-header>
      <CustomerDetail :getList="getList" :recordList="recordList" :getCustomerChangeList="getCustomerChangeList"  ref="customerDetail" :currentType="currentType"
                      :bzRecordList="bzRecordList" :commentRecordList="commentRecordList"
                      :form="form" :pushRecordList="pushRecordList" :returnRecordList="returnRecordList"
                      :channelCustomerList="channelCustomerList"
                      :customerChangeList="customerChangeList"></CustomerDetail>
    </el-dialog>

    <!-- 退回原因    -->
    <back-return ref="backReturn"></back-return>
    <!-- 拉取客户   -->
    <pull-superior-customer ref="pullSuperiorCustomer"></pull-superior-customer>

    <!-- 分配操作 -->
    <transfer-customer ref="transferCustomer"></transfer-customer>

    <bath-dis-customer ref="bathDisCustomer"></bath-dis-customer>
  </el-dialog>
</template>

<script>
import {
  listCustomer,
  getCustomer,
  delCustomer,
  loadParentOrg,
  customerRL,
  saveBz,
  getMxInfo, queryInfoList, getRecordList, getCustomerStar, invite, upDoor, pushCustomerRecord, returnCustomerRecord
} from "@/api/customer/customer";
import BackReturn from "@/views/components/BackReturn.vue";
import PullSuperiorCustomer from "@/views/components/PullSuperiorCustomer.vue";
import TransferCustomer from "@/views/components/TransferCustomer.vue";
import CustomerDetail from '@/components/CustomerDetail'
import {atTeamCustomerList, queryInfoListTeam, teamUserList} from "@/api/teamCustomer/teamCustomer";
import BathDisCustomer from "./BathDisCustomer.vue";
import Treeselect from "@riophae/vue-treeselect";
import {deptTreeSelect, nolistUser} from "@/api/system/user";
import {noListCommentRecord} from "@/api/customer/commentRecord";
import {noListChannelDataConf} from "@/api/channel/channelDataConf";
import {channelCustomerList} from "@/api/channel/channelPushDis";
import {getCustomerInfoChangeList} from "@/api/customer/customerInfoChange";

export default {
  name: "StarCustomer",
  components: {
    Treeselect,
    BathDisCustomer,
    BackReturn,
    PullSuperiorCustomer,
    CustomerDetail,
    TransferCustomer
  },
  dicts: ['fund_type', 'customer_status', 'sex_type', 'car_type', 'insurance_type', 'house_type', 'warranty_type',
    'level_type', 'loan_type', 'company_status', 'quota_type', 'is_vip', 'time_type', 'apply_time_scope',
    'source_type'],
  data() {
    return {
      starTitle: '',
      starOpen: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 省级地址
      provinceList: [],
      // 市区地址
      searchCityList: [],
      // 区县
      searchCountyList: [],
      // 市区地址
      formCityList: [],
      // 区县
      formCountyList: [],
      // 记录信息
      bzRecordList: [],
      // 部门树选项
      deptOptions: [],
      // 用户选项
      userOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        name: undefined,
        tel: undefined,
        level: undefined,
        loanType: undefined,
        age: undefined,
        sex: undefined,
        label: undefined,
        education: undefined,
        house: undefined,
        car: undefined,
        warranty: undefined,
        insurance: undefined,
        fund: undefined,
        salary: undefined,
        applyMoney: undefined,
        province: undefined,
        city: undefined,
        county: undefined,
        manager: undefined,
        customerStatus: undefined,
        companyStatus: undefined,
        distributeDeptId: undefined,
        share: undefined,
        source: undefined,
        dataSource: undefined,
        vip: undefined,
        taskId: undefined,
        createDate: undefined,
        bz: undefined,
        notUpdateDay: undefined, // 未超出天数
        timeType: undefined,  // 时间类型
        applyTimeScope: undefined, // 申请时间
        startTime: undefined, // 自定义开始时间
        endTime: undefined, // 自定义结束时间
        currentType: undefined,
        starType: undefined,
        revieweFlag: undefined,
        busUserId: undefined,
        childrenDept: undefined,
        batchLogo: undefined,
        sortInfo: undefined,
        sortRule: undefined,
        channelId: undefined
      },

      // 记录信息
      bzOpen: false,
      bzForm: {},
      bzRules: {
        bz: [
          {required: true, message: "记录不能为空", trigger: "blur"}
        ],
      },

      // 明细信息
      mxOpen: false,
      mxForm: {},

      // 上一页 下一页实现
      operList: [], //  全量id  [{id:'1'},{id:'2'}] 这种形式
      index: 0, // 当前页面下标
      pageId: '', // 当前页面id

      // 表单参数
      form: {},

      star: {
        new: '',
        one: '',
        two: '',
        three: ''
      },

      // 当前页面操作类型
      currentType: '',
      // 星级类型
      starType: '',
      teamUserList: [],

      pushRecordList: [], // 下推记录
      returnRecordList: [], // 退回记录
      commentRecordList: [], // 点评记录
      channelCustomerList: [], // 渠道客户记录
      customerChangeList: [], // 客户信息改变记录
      pushRecordListStr: "",

      userQueryParam: {
        deptId: undefined,
        ancestors: undefined
      },

      channelDataList: [],

    };
  },
  created() {
    // 客户提醒
    //this.getCustomerStarInit();
  },
  methods: {
    // 获取渠道数据信息
    getChannelDataList() {
      noListChannelDataConf().then(res => {
        this.channelDataList = res.data;
      });
    },
    sortChange(column) {
      const sortInfo = column.prop  //  当前排序的字段
      let order = 'desc';
      if (column.order === 'descending') {
        order = 'desc' // 排序或倒序
      } else {
        order = 'asc' // 排序或倒序
      }
      this.queryParams.sortInfo = sortInfo;
      this.queryParams.sortRule = order;
      this.getList(this.currentType, this.starType);
    },


    /** 打开弹窗按钮 */
    openDialog(currentType, starType) {
      this.currentType = currentType;
      this.starType = starType;
      this.starOpen = true;
      this.resetQuery();
      this.resetForm("queryForm");
      this.loadPOrg('0', 0, 's')
      this.getChannelDataList();
      // 跟进页面类型+星级类型 查询对应的列表数据 重复调用
      //this.getList(this.currentType, this.starType);
      //this.operInfoList(this.currentType, this.starType);
    },

    /* 用户弹框关闭后刷新当前弹框信息 */
    refreshList() {
      this.resetQuery();
      this.resetForm("queryForm");
    },

    getCustomerStarInit() {
      getCustomerStar("1").then(resp => {
        this.star = resp.data;
        this.$confirm(
            '客户提醒：你有待跟进客户,新数据' + this.star.new
            + '条, 超过1天3星待签约' + (this.currentType == 'claim' ? 0 : this.star.three) + '条, 超过3天2,4,5,6星待签约'
            + (this.currentType == 'claim' ? 0 : this.star.other)
            + '条,超过7天1星待签约' + (this.currentType == 'claim' ? 0 : this.star.one) + '条',
            {
              confirmButtonText: '确定', //确认按钮的文字
              showCancelButton: false, //是否显示取消按钮
              showClose: false, //是否显示关闭按钮
              closeOnClickModal: false, //是否可以通过点击空白处关闭弹窗
              type: 'info'
            }).then(() => {
          this.loading = false;
          //this.$modal.msgSuccess("已阅读提醒");
        }).catch(() => {
        }).finally(() => {
          this.loading = false;
        })
      })
    },
    /**  日期时间触发改变 */
    applyTimeScopeChange(val) {
      if (val != 99) {
        this.queryParams.startTime = undefined;
        this.queryParams.endTime = undefined;
      }
    },
    operInfoList(currentType, starType) {
      this.queryParams.currentType = currentType;
      this.queryParams.starType = starType;
      if (this.currentType == 'teamCustomer') {
        queryInfoListTeam(this.queryParams).then(response => {
          this.operList = response.data;
        });
      } else {
        queryInfoList(this.queryParams).then(response => {
          this.operList = response.data;
        });
      }
    },
    // 上一篇
    handlePrevious() {
      this.index = this.operList.findIndex(item => item.id === this.pageId)
      if (this.index === 0) {
        // 提示已经是第一篇或者禁用上一篇按钮 自由发挥咯
      } else {
        this.pageId = this.operList[this.index - 1].id // 获取上一篇id 在getList()里需要
        this.getInfoList() // 获取详情数据的接口
      }
    },
    // 下一篇
    handleNext() {
      this.index = this.operList.findIndex(item => item.id === this.pageId)
      if (this.index >= this.operList.length - 1) {
        // 提示已经是最后一篇或者禁用下一篇按钮 自由发挥咯
      } else {
        this.pageId = this.operList[this.index + 1].id // 获取下一篇id 在getList()里需要
        this.getInfoList() // 获取详情数据的接口
      }
    },
// 获取详情数据 todo  待添加类型
    getInfoList() {
      this.reset();
      getCustomer(this.pageId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        // 回选区域信息
        if (this.form.province) {
          this.loadPOrg(this.form.province, 1, "f");
        } else {
          this.searchCityList = [];
        }
        if (this.form.city) {
          this.loadPOrg(this.form.city, 2, "f");
        } else {
          this.searchCountyList = [];
        }
        this.title = "修改客户信息";
      });
      // 获取右侧备注信息
      getRecordList(this.pageId).then(response => {
        this.bzRecordList = response.data;
      })
      //getMxInfo(this.pageId).then(res => {
      //  this.mxForm = res.data;
      //  this.mxOpen = true;
      //  this.title = "明细信息";
      //})
      this.pushCustomerRecord(this.pageId);
      this.returnCustomerRecord(this.pageId);
      this.getCommentRecordList(this.pageId);
      this.getChannelCustomerList(this.pageId);
      this.getCustomerChangeList(this.pageId);
    },

    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.childrenDept,
        label: node.label,
        children: node.children
      }
    },

    /** 查询用户信息 */
    getQueryUserList(queryParam) {
      nolistUser(queryParam).then(response => {
        this.userOptions = response.data;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 加载组织 **/
    loadPOrg(parentId, level, type) {
      loadParentOrg(parentId).then(response => {
        if (level == '0') {
          this.provinceList = response.data
        } else if (level == '1') {
          if (type === 's') {
            this.searchCityList = response.data
          } else {
            this.formCityList = response.data
          }
        } else {
          if (type == 's') {
            this.searchCountyList = response.data
          } else {
            this.formCountyList = response.data
          }
        }
      })
    },
    getChildren(parentId, level, type) {
      this.clearSelect(level, type)
      this.loadPOrg(parentId, level, type)
    },

    clearSelect(level, type) {
      if (type == 's') {
        if (level == '1') {
          this.searchCityList = []
          this.queryParams.city = ''
        }
        this.queryParams.county = ''
        this.searchCountyList = []
      } else {
        if (level == '1') {
          this.formCityList = []
          this.form.city = ''
        }
        this.form.county = ''
        this.formCountyList = []
      }
    },
    /** 下推记录查看 */
    pushCustomerRecord(id) {
      pushCustomerRecord(id).then(res => {
        this.pushRecordList = res.data;
      })
    },

    returnCustomerRecord(id) {
      returnCustomerRecord(id).then(res => {
        this.returnRecordList = res.data;
      })
    },

    getCommentRecordList(id) {
      const queryWrapper = {
        customerId: id
      }
      noListCommentRecord(queryWrapper).then(res => {
        this.commentRecordList = res.data;
      })
    },

    getChannelCustomerList(id) {
      const queryWrapper = {
        id: id
      };
      channelCustomerList(queryWrapper).then(res => {
        this.channelCustomerList = res.data;
      })
    },

    getCustomerChangeList(id) {
      const queryWrapper = {
        customerId: id,
        type: 'customerInfoChange'
      };
      getCustomerInfoChangeList(queryWrapper).then(res => {
        this.customerChangeList = res.data;
      })
    },


    /** 查询客户信息列表 */
    getList(currentType, starType) {
      this.loading = false;
      // 我的客户
      if (currentType == 'myCustomer') {
        this.queryParams.currentType = currentType;
        this.queryParams.starType = starType;
        listCustomer(this.queryParams).then(response => {
          this.customerList = response.rows;
          this.operList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        // 再分配客户
      } else if (currentType == 'claim') {
        this.queryParams.currentType = currentType;
        this.queryParams.starType = starType;
        listCustomer(this.queryParams).then(response => {
          this.customerList = response.rows;
          this.operList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        // 团队客户
      } else if (currentType == 'teamCustomer') {
        this.queryParams.currentType = currentType;
        this.queryParams.starType = starType;
        atTeamCustomerList(this.queryParams).then(response => {
          this.customerList = response.rows;
          this.operList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    bzCancel() {
      this.bzOpen = false;
      this.bzRest();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      // this.getList(this.currentType, this.starType)
    },
    bzRest() {
      this.bzForm = {
        id: undefined,
        bz: undefined,
      }
      this.resetForm("bzForm");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        tel: undefined,
        level: undefined,
        loanType: undefined,
        age: undefined,
        sex: undefined,
        label: undefined,
        education: undefined,
        house: undefined,
        car: undefined,
        warranty: undefined,
        insurance: undefined,
        fund: undefined,
        salary: undefined,
        applyMoney: undefined,
        province: undefined,
        city: undefined,
        county: undefined,
        remark: undefined,
        manager: undefined,
        customerStatus: undefined,
        companyStatus: undefined,
        distributeDeptId: undefined,
        share: undefined,
        source: undefined,
        dataSource: undefined,
        vip: undefined,
        taskId: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateTime: undefined,
        bz: undefined,
        htList: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.currentType, this.starType);
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.form.vip) {
        this.form.vip = undefined;
      }
      this.searchCityList = [];
      this.searchCountyList = [];
      this.queryParams.sortInfo = undefined;
      this.queryParams.sortRule = undefined;
      this.resetForm("queryForm");
      this.handleQuery();

      // 团队客户
      // if (this.currentType == 'teamCustomer') {
      this.getDeptTree();
      this.userQueryParam = {
        deptId: undefined,
        ancestors: undefined
      }
      // 初始化用户信息
      this.getQueryUserList(this.userQueryParam);
      // }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 拉取客户 */
    handlePull() {
      this.$refs.pullSuperiorCustomer.openDialog('customerPull');
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },

    /** 部门改变 */
    deptChange(val, type) {
      this.queryParams.busUserId = "";

      // 重置用户信息
      if (val) {
        if ("deptId" == type) {
          this.userQueryParam.ancestors = undefined;
          this.userQueryParam.deptId = val;
        } else if ("childrenDept" == type) {
          this.userQueryParam.ancestors = val;
          this.userQueryParam.deptId = undefined;
        }
        this.getQueryUserList(this.userQueryParam);
      }
    },
    /** 邀约 */
    upDoorInfo(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认上门客户编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return upDoor(ids);
      }).then(() => {
        this.loading = false;
        this.getList(this.currentType, this.starType);
        this.$modal.msgSuccess("上门成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      })
    },
    /** 客户转移 */
    bathHandleTransfer() {
      const ids = this.ids;
      if (ids.length < 1) {
        this.$modal.msgError("请选择客户");
        return;
      }
      const customerIds = []
      ids.forEach(item => {
        const obj = this.customerList.find(it => it.id === item);
        const data = {
          "id": obj.id,
          "taskId": obj.taskId,
          "name": obj.name
        }
        customerIds.push(data);
      })
      this.$refs.bathDisCustomer.openDialog(customerIds, 'teamCustomer');
    },

    /** 客户转移 */
    handleTransfer(row) {
      const id = row.id || this.ids;
      const customerIds = []
      const obj = this.customerList.find(it => it.id === id);
      const data = {
        "id": obj.id,
        "taskId": obj.taskId,
        "name": obj.name
      }
      customerIds.push(data);
      this.$refs.bathDisCustomer.openDialog(customerIds, 'teamCustomer');
    },

    getTeamUserList() {
      teamUserList().then(res => {
        if (res.code == 200) {
          this.teamUserList = res.data;
        }
      })
    },
    /** 邀约 */
    inviteInfo(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认邀约客户编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return invite(ids);
      }).then(() => {
        this.loading = false;
        this.getList(this.currentType, this.starType);
        this.$modal.msgSuccess("邀约成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      })
    },

    /** 记录备注 */
    handleRecordBZ(row) {
      this.bzOpen = true;
      this.title = "添加记录";
      this.bzRest();
      this.loading = true;
      const id = row.id || this.ids
      getCustomer(id).then(response => {
            this.loading = false;
            this.bzForm = response.data;
          }
      )
    },

    handleRowClick(row) {
      this.handleMx(row)
    },

    /** 明细 */
    handleMx(row) {
      const ids = row.id || this.ids;
      getMxInfo(ids).then(resp => {
        this.mxForm = resp.data;

        this.mxOpen = true;
        this.title = "明细信息";
      })
    },

    tanHandleRl() {
      const ids = this.pageId;
      this.$modal.confirm('是否确认认领客户信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return customerRL(ids);
      }).then(res => {
        const msg = res.data;
        this.loading = false;
        this.getList(this.currentType, this.starType);
        if (msg.info) {
          this.$alert(msg.info, '认领信息', {
            dangerouslyUseHTMLString: true,
            confirmButtonText: '确定',
          });
        } else {
          this.$modal.msgSuccess("认领成功");
        }
        this.handleNext();
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      })
    },

    /** 客户认领 */
    handleRL(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认认领客户信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return customerRL(ids);
      }).then(res => {
        const msg = res.data;
        this.loading = false;
        this.getList(this.currentType, this.starType);
        if (msg.info) {
          this.$alert(msg.info, '认领信息', {
            dangerouslyUseHTMLString: true,
            confirmButtonText: '确定',
          });
        } else {
          this.$modal.msgSuccess("认领成功");
        }
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      })
    },

    handlePLRL() {
      const ids = this.ids;
      if (ids.length < 1) {
        this.$modal.msgError("请选择客户");
        return;
      }
      this.$modal.confirm('是否确认认领客户信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return customerRL(ids);
      }).then(res => {
        const msg = res.data;
        this.loading = false;
        this.getList(this.currentType, this.starType);
        if (msg.info) {
          this.$alert(msg.info, '认领信息', {
            dangerouslyUseHTMLString: true,
            confirmButtonText: '确定',
          });
        } else {
          this.$modal.msgSuccess("认领成功");
        }
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      })
    },

    /** 退回： 1、批量退回  3、修改页面退回**/
    customerReturn(type) {
      const customerIds = []
      if (type == '1') {
        const ids = this.ids;
        if (ids.length < 1) {
          this.$modal.msgError("请选择客户");
          return;
        }
        ids.forEach(item => {
          const obj = this.customerList.find(it => it.id === item);
          const data = {
            "id": obj.id,
            "parentDeptId": obj.parentDeptId,
            "taskId": obj.taskId
          }
          customerIds.push(data);
        })
      } else if (type == '3') {
        const obj = this.customerList.find(it => it.id == this.pageId);
        const data = {
          "id": obj.id,
          "parentDeptId": obj.parentDeptId,
          "taskId": obj.taskId
        }
        customerIds.push(data);
      }
      this.$refs.backReturn.openDialog(customerIds, "客户退回", 'myCustomer');
    },

    /** 客户退回 */
    handleReturn(row) {
      const id = row.id || this.ids;
      // this.buttonLoading = true;
      const customerIds = []
      const obj = this.customerList.find(it => it.id === id);
      const data = {
        "id": obj.id,
        "parentDeptId": obj.parentDeptId,
        "taskId": obj.taskId
      }
      customerIds.push(data);
      this.$refs.backReturn.openDialog(customerIds, "客户退回", 'myCustomer');
    },

    cellClick(row, column, cell, event) {
      if (column.property == 'customerStatus') {
        this.handleUpdate(row)
      }
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      this.pageId = id;
      getCustomer(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        // 回选区域信息
        if (this.form.province) {
          this.loadPOrg(this.form.province, 1, "f");
        } else {
          this.searchCityList = [];
        }
        if (this.form.city) {
          this.loadPOrg(this.form.city, 2, "f");
        } else {
          this.searchCountyList = [];
        }
        this.title = "修改客户信息";
      });
      // 获取右侧信息
      this.recordList(id);
      this.pushCustomerRecord(id);
      this.returnCustomerRecord(id);
      this.getCommentRecordList(id);
      this.getChannelCustomerList(id);
      this.getCustomerChangeList(id);
    },
    /** 获取右侧跟进记录 */
    recordList(id) {
      // 获取右侧备注信息
      getRecordList(id).then(response => {
        this.bzRecordList = response.data;
      })
    },
    /** 保存备注 */
    submitRecord() {
      this.$refs["bzForm"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          saveBz(this.bzForm).then(response => {
            this.$modal.msgSuccess("记录成功");
            this.bzOpen = false;
            this.getList(this.currentType, this.starType);
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['customerDetail'].btnSubmit()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delCustomer(ids);
      }).then(() => {
        this.loading = false;
        this.getList(this.currentType, this.starType);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    },
    /** 弹框关闭 */
    closeDialog() {
      this.starOpen = false;
      if (this.$parent.getList()) {
        this.$parent.getList(this.currentType, this.starType);
      }
    },
    /** 修改框 */
    ecCloseDialog() {
      this.open = false;
      this.getList(this.currentType, this.starType);
    },
  }
};
</script>
<style scoped>
.messageCard {
  overflow-x: hidden;
  overflow-y: scroll;
  word-break: break-all;
  width: 400px;
  max-height: 100px;
  text-align: left;
  float: left;
  text-align: left;
  text-overflow: ellipsis;
  color: #313131;
}

.messageCard::-webkit-scrollbar {
  width: 4px;
}

.messageCard::-webkit-scrollbar-thumb {
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.2);
}

.messageCard::-webkit-scrollbar-track {
  border-radius: 0;
  background: rgba(0, 0, 0, 0.1);
}
</style>
<style type="text/css">
.el-dialog__body {
  padding: 5px 20px 30px 20px;
}

.el-form-item {
  margin-bottom: 8px;
}

.el-form-item--small.el-form-item {
  margin-bottom: 8px;
}

.el-timeline-item {
  padding-bottom: 1px;
}

</style>
